<?php
include("../includes/DBFunctions.php");
include("../includes/getCredentials.php");

function environmentReportFunctor($envid) {
	$db = new DBFunctions;
	$gc = new GetCredentials;
	$uid = $gc->getUserID();
	$db->connectDB();
	$query = "call environmentReport(".$envid.",".$uid.",@a);";
	$result = $db->query($query);
	if( !$result ) {
		die("alert('Failed stored procedure call (environmentReport)');");
	}
	if (mysqli_num_rows($result) != 1) {
		$db->closeDB();
		$toPrint .= "<div><h2>n/a</h2></div>";
	} else {
		while( $line = mysqli_fetch_array( $result, MYSQL_ASSOC ) ) {
			$toPrint = "<div><h3>".$line['Name']."</h3><ul>";
			$toPrint .= "<li>Yearly Rain: ".$line['Precipitation']." mm/yr</li>";
			$toPrint .= "<li>Avg. Heat: ".$line['Heat']."&deg; F</li>";
			$toPrint .= "<li>Yearly Sunlight: ".$line['Sunlight']." hrs/yr</li>";
			$toPrint .= "</ul></div>";
		}
		$db->closeDB();
	}
	
	echo 'displayReport("'.$toPrint.'");';
}

if( !isset($_POST['envid']) || $_POST['envid'] == '') {
	die("alert('No environment was selected. Please try again.');");
}
$envid = $_POST['envid'];
environmentReportFunctor($envid);
?>